home *** CD-ROM | disk | FTP | other *** search
/ PD Collection CD 1 / PD Collection CD 1.iso / textual / tex / files / !tex / TeXsource / commontex / c / arith < prev    next >
Encoding:
Text File  |  1988-04-08  |  2.2 KB  |  169 lines

  1. /*
  2.  *    Copyright 1986, 1987 Pat Joseph Monardo. All rights reserved.
  3.  *    Copying of this file is granted according to the provisions 
  4.  *    specified in the file COPYING which must accompany this file.
  5.  */
  6.  
  7.  
  8. /*
  9.  *        arith.c
  10.  */
  11.  
  12. #include "tex.h"
  13. #include "print.h"
  14. #include "arith.h"
  15.  
  16. bool    arith_error;
  17. scal    remainder;
  18.  
  19. val 
  20. half (x)
  21.     val        x;
  22. {
  23.     return (odd(x) ? (x + 1) / 2 : x / 2);
  24. }
  25.  
  26.  
  27. scal
  28. round_decimals (k)
  29.     int        k;
  30. {
  31.     val        a;
  32.  
  33.     a = 0;
  34.     while (k > 0) {
  35.         decr(k);
  36.         a = (a + dig[k] * TWO) / 10;
  37.     }
  38.     return ((a + 1) / 2);
  39. }
  40.  
  41. print_scaled (s)
  42.     scal    s;
  43. {
  44.     scal    delta;
  45.  
  46.     if (s < 0) {
  47.         print_char('-');
  48.         negate(s);
  49.     }
  50.     print_val(s / UNITY);
  51.     print_char('.');
  52.     s = 10 * (s % UNITY) + 5;
  53.     delta = 10;
  54.     do {
  55.         if (delta > UNITY)
  56.             s += 0100000 - (delta / 2);
  57.         print_char('0' + s / UNITY);
  58.         s = 10 * (s % UNITY);
  59.         delta *= 10;
  60.     } while (s > delta);
  61. }
  62.  
  63. scal
  64. nx_plus_y (n, x, y)
  65.     val        n;
  66.     scal    x;
  67.     scal    y;
  68. {
  69.     if (n < 0) {
  70.         negate(x);
  71.         negate(n);
  72.     }
  73.     if (n == 0)
  74.         return y;
  75.     else if (x <= (07777777777 - y) / n &&
  76.             -x <= (07777777777 + y) / n)
  77.         return (n * x + y);
  78.     else {
  79.         arith_error = TRUE;
  80.         return 0;
  81.     }
  82. }
  83.  
  84. scal
  85. x_over_n (x, n)
  86.     scal    x;
  87.     val        n;
  88. {
  89.     bool    negative;
  90.     scal    quotient;
  91.  
  92.     negative = FALSE;
  93.     if (n == 0) {
  94.         arith_error = TRUE;
  95.         remainder = x;
  96.         return 0;
  97.     }
  98.     if (n < 0) {
  99.         negate(x);
  100.         negate(n);
  101.         negative = TRUE;
  102.     }
  103.     if (x >= 0) {
  104.         quotient = x / n;
  105.         remainder = x % n;
  106.     } else {
  107.         quotient = -(-x / n);
  108.         remainder = -(-x % n);
  109.     }
  110.     if (negative)
  111.         negate(remainder);
  112.     return quotient;
  113. }
  114.  
  115. scal
  116. xn_over_d (x, n, d)
  117.     scal    x;
  118.     val        n;
  119.     val        d;
  120. {
  121.     val        t;
  122.     val        u;
  123.     val        v;
  124.     bool    positive;
  125.  
  126.     if (x >= 0)
  127.         positive = TRUE;
  128.     else {
  129.         negate(x);
  130.         positive = FALSE;
  131.     }
  132.     t = (x % 0100000) * n;
  133.     u = (x / 0100000) * n + (t / 0100000);
  134.     v = (u % d) * 0100000 + (t % 0100000);
  135.     if (u / d >= 0100000)
  136.         arith_error = TRUE;
  137.     else u = 0100000 * (u / d) + (v / d);
  138.     if (positive) {
  139.         remainder = v % d;
  140.         return u;
  141.     } else {
  142.         remainder = - (v % d);
  143.         return -u;
  144.     }
  145. }
  146.  
  147. hword
  148. badness (t, s)
  149.     scal    t;
  150.     scal    s;
  151. {
  152.     val        r;
  153.  
  154.     if (t == 0)
  155.         return 0;
  156.     else if (s <= 0)
  157.         return INF_BAD;
  158.     else {
  159.         if (t <= 7230584)
  160.             r = (t * 297) / s;
  161.         else if (s >= 1663497)
  162.             r = t / (s / 297);
  163.         else r = t;
  164.         if (r > 1290)
  165.             return INF_BAD;
  166.         else return ((r * r * r + 0400000) / 01000000);
  167.     }
  168. }
  169.